package org.com.yz.dubbo.web.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.com.yz.common.annotion.Parameter;
import org.com.yz.common.constant.PayConstant;
import org.com.yz.common.util.ClassRefUtil;
import org.com.yz.common.util.MyLog;
import org.com.yz.common.util.ToolUtil;
import org.com.yz.dal.dao.model.Accnt;
import org.com.yz.dal.dao.model.DispatchInfo;
import org.com.yz.dal.dao.model.DispatchPayInfo;
import org.com.yz.dubbo.web.service.RpcCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 分账接口控制类
 *
 * @author
 * @date
 */
@RestController
public class DispatchMchAccntController {

    private final MyLog _log = MyLog.getLog(DispatchMchAccntController.class);
    @Autowired
    private RpcCommonService rpcCommonService;


    @PostMapping(value = "/api/dispatchMchAccnt.htm")
    @Parameter
    public String dispatchMchAccnt(String data, String sign) {
        String result = StringEscapeUtils.unescapeJava(data);
        DispatchInfo dispatchReq = JSONObject.parseObject(result, DispatchInfo.class);
        DispatchInfo dispatchRes = new DispatchInfo();
        try {
            //执行分账逻辑
            Map<String, List<Accnt>> resultMap = rpcCommonService.dispatchMchAccntService.dispatch(data);
            if (resultMap.get("split_accnt_detail") == null || ToolUtil.isNotEmpty(resultMap.get("errMsg"))) {
                dispatchRes.setBizContent(null);
                _log.info("分账失败");
                return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_DISPATCH_ERROR, PayConstant.RETURN_DEPOSIT_DISPATCH_ERRORS+":{"+resultMap.get("errMsg")+"}");
            }
            dispatchRes.setBizContent(resultMap);
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        } catch (Exception e) {
            _log.error(e, ToolUtil.getExceptionMsg(e));
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
        }
    }

    /**
     * 商户支付记账
     * @param data
     * @param sign
     * @return
     */
    @PostMapping(value = "/api/dispatchPayMchAccnt.htm")
    @Parameter
    public String dispatchPayMchAccnt(String data, String sign) {
        String result = StringEscapeUtils.unescapeJava(data);
        DispatchPayInfo dispatchReq = JSONObject.parseObject(result, DispatchPayInfo.class);
        DispatchPayInfo dispatchRes = new DispatchPayInfo();
        try {
            //执行分账逻辑
            Map<String, Object> resultMap = rpcCommonService.dispatchMchAccntService.dispatchPay(data);
            if (resultMap.get("split_accnt_detail") == null || ToolUtil.isNotEmpty(resultMap.get("errMsg"))) {
                dispatchRes.setBizContent(null);
                _log.info("分账失败");
                return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_DISPATCH_ERROR, PayConstant.RETURN_DEPOSIT_DISPATCH_ERRORS+":{"+resultMap.get("errMsg")+"}");
            }
            dispatchRes.setBizContent(resultMap);
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        } catch (Exception e) {
            _log.error(e, ToolUtil.getExceptionMsg(e));
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
        }
    }

    /**
     * 商户退款记账
     * @param data
     * @param sign
     * @return
     */
    @PostMapping(value = "/api/dispatchRefundMchAccnt.htm")
    @Parameter
    public String dispatchRefundMchAccnt(String data, String sign) {
        String result = StringEscapeUtils.unescapeJava(data);
        DispatchPayInfo dispatchReq = JSONObject.parseObject(result, DispatchPayInfo.class);
        DispatchPayInfo dispatchRes = new DispatchPayInfo();
        try {
            //执行分账逻辑
            Map<String, Object> resultMap = rpcCommonService.dispatchMchAccntService.dispatchRefund(data);
            if (resultMap.get("split_accnt_detail") == null || ToolUtil.isNotEmpty(resultMap.get("errMsg"))) {
                dispatchRes.setBizContent(null);
                _log.info("分账失败");
                return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_DISPATCH_ERROR, PayConstant.RETURN_DEPOSIT_DISPATCH_ERRORS+":{"+resultMap.get("errMsg")+"}");
            }
            dispatchRes.setBizContent(resultMap);
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        } catch (Exception e) {
            _log.error(e, ToolUtil.getExceptionMsg(e));
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
        }
    }

    /**
     * 商户非支付退款类记账
     * @param data
     * @param sign
     * @return
     */
    @PostMapping(value = "/api/dispatchOtherMchAccnt.htm")
    @Parameter
    public String dispatchOtherMchAccnt(String data, String sign) {
        String result = StringEscapeUtils.unescapeJava(data);
        DispatchPayInfo dispatchReq = JSONObject.parseObject(result, DispatchPayInfo.class);
        DispatchPayInfo dispatchRes = new DispatchPayInfo();
        try {
            //执行分账逻辑
            Map<String, Object> resultMap = rpcCommonService.dispatchMchAccntService.dispatchOther(data);
            if (resultMap.get("split_accnt_detail") == null || ToolUtil.isNotEmpty(resultMap.get("errMsg"))) {
                dispatchRes.setBizContent(null);
                _log.info("分账失败");
                return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_DISPATCH_ERROR, PayConstant.RETURN_DEPOSIT_DISPATCH_ERRORS+":{"+resultMap.get("errMsg")+"}");
            }
            dispatchRes.setBizContent(resultMap);
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        } catch (Exception e) {
            _log.error(e, ToolUtil.getExceptionMsg(e));
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
        }
    }

    @PostMapping(value = "/api/dispatchMchAccntRecon.htm")
    @Parameter
    public String dispatchMchAccntRecon(String data, String sign) {
        String result = StringEscapeUtils.unescapeJava(data);
        DispatchInfo dispatchReq = JSONObject.parseObject(result, DispatchInfo.class);
        DispatchInfo dispatchRes = new DispatchInfo();
        try {
            //执行分账逻辑
            Map<String, List<Accnt>> resultMap = rpcCommonService.dispatchMchAccntService.dispatchRecon(data);
            if (resultMap.get("split_accnt_detail") == null || ToolUtil.isNotEmpty(resultMap.get("errMsg"))) {
                dispatchRes.setBizContent(null);
                _log.info("分账失败");
                return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_DISPATCH_ERROR, PayConstant.RETURN_DEPOSIT_DISPATCH_ERRORS+":{"+resultMap.get("errMsg")+"}");
            }
            dispatchRes.setBizContent(resultMap);
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_SUCESS, PayConstant.RETURN_ALIPAY_VALUE_SUCCESS);
        } catch (Exception e) {
            _log.error(e, ToolUtil.getExceptionMsg(e));
            return ClassRefUtil.createRetData(dispatchReq, dispatchRes, PayConstant.RETURN_DEPOSIT_CODE_ERROR, PayConstant.RETURN_DEPOSIT_CODES_ERROR);
        }
    }

}
